{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1f991927-ab89-4c9c-881e-75f9df0c5ea6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import serial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b685f512-0f63-47b8-8bdc-497eeec24050",
   "metadata": {},
   "outputs": [],
   "source": [
    "def to_labbrick_readable_freq(freq, mode=0):\n",
    "    if mode==0:\n",
    "        out = \"F\"+format(format(freq,\".2f\"),\"0>7s\")[-7:]\n",
    "    if mode==1:\n",
    "        out = \"F\"+format(format(freq,\".2f\"),\"0>8s\")[-8:]\n",
    "    return out\n",
    "\n",
    "def to_labbrick_readable_amp(amp):\n",
    "    return \"A\"+format(format(amp,\".0f\"),\"0>2s\")[-2:]+\"ATTEN\"\n",
    "\n",
    "class Labbrick:\n",
    "    def __init__(self, serial_port = None, mode=0):\n",
    "        self.port = serial_port\n",
    "        self.mode = mode # 0 represents ordinary mode; 1 represents high frequency mode (used for generate 13GHz)\n",
    "        \n",
    "        if serial_port != None:\n",
    "            self.ser  = serial.Serial(\n",
    "                port=serial_port,\n",
    "                baudrate=9600,\n",
    "                parity=serial.PARITY_NONE,\n",
    "                stopbits=serial.STOPBITS_TWO,\n",
    "                bytesize=serial.EIGHTBITS\n",
    "            )\n",
    "            \n",
    "    def freq_update(self, freq):\n",
    "        str_to_send = to_labbrick_readable_freq(freq, self.mode).encode('utf_8')\n",
    "        self.ser.write(str_to_send)\n",
    "        \n",
    "    def amp_update(self, amp):\n",
    "        if(amp > 0 or amp < -30):\n",
    "            raise AssertionError(\"The amp of domestic Labbrick should in [-30, 0], which represents the attenuation of the output. Check Feishu for detail.\")\n",
    "        else:\n",
    "            str_to_send = to_labbrick_readable_amp(abs(amp)).encode('utf_8')\n",
    "            self.ser.write(str_to_send)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "61e357ce-be56-40ba-956d-2b0667c5b660",
   "metadata": {},
   "outputs": [],
   "source": [
    "labbrick = Labbrick(serial_port = \"COM7\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "66ae8471-55f6-4681-8d22-48be7cee3a80",
   "metadata": {},
   "outputs": [],
   "source": [
    "labbrick.freq_update(180)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bf3b2cdc-21d4-4975-891e-b67e7ef57432",
   "metadata": {},
   "outputs": [],
   "source": [
    "labbrick.amp_update(-10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4c6e82d9-6107-435b-89f0-6b62e5614837",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'F13382.00'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "to_labbrick_readable_freq(13382,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4dadec9f-53eb-42e5-aaba-6356b880b818",
   "metadata": {},
   "outputs": [],
   "source": [
    "labbrick = Labbrick(serial_port = \"COM4\", mode=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "841bb09d-cdf9-415e-9837-997f53f084ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "labbrick.freq_update(13382)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f295aa1b-a29b-44e0-8c4d-f80d2d8c4b40",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
